var cityParent = null;
var countyParent = null;
var finalid = null;
//选择地区的模块
function chooseRegion(){
    var province = document.getElementById("province");
    var city = document.getElementById("city");
    var county = document.getElementById("county");
    var hideClass = document.getElementById("ssq");
    var orOnce1 = true, orOnce2 = true, orOnce3 = true;
    Tap(province,function(e,dom){
        dom.nextSibling.nextSibling.style.transform = "rotate(-180deg)";
        if(orOnce1){
            orOnce1 = false;
            var distence = dom.offsetTop + 42;
            hideClass.scrollTop = 0;
            hideClass.style.height = "314px";
            hideClass.style.top = distence + "px";
            ajax({
                url:"global_region.json",
                method:"get",
                dataType:"json",
                success:function(data){
                    var obj = JSON.parse(data);
                    var arr = obj.RECORDS;
                    var html = "";
                    for(var i = 0; i < arr.length; i++){
                        if(arr[i].region_type == 1){
                            html += '<div data-parentId="'+arr[i].parent_id+'" data-regionId="'+arr[i].region_id+'">'+arr[i].region_name+'</div>';
                        }
                    }
                    hideClass.innerHTML = html;
                    var provinces = hideClass.querySelectorAll("div");
                    for(var j = 0; j < provinces.length; j++){
                        Tap(provinces[j],function(e,dom){
                            var m1 = document.querySelector(".m1")
                            m1.style.transform = "rotate(0)";
                            var provName = dom.innerHTML;
                            province.innerHTML = provName;
                            cityParent = dom.getAttribute("data-regionId");
                            hideClass.style.height = "0";
                            city.innerHTML = "请选择城市";
                            county.innerHTML = "请选择区县";
                            countyParent = null;
                            finalid = null;
                        });
                    }
                }
            });
        }else{
            orOnce1 = true;
            hideClass.style.height = "0";
            dom.nextSibling.nextSibling.style.transform = "rotate(0)";
        }
    })
    Tap(city,function(e,dom){
        dom.nextSibling.nextSibling.style.transform = "rotate(-180deg)";
        if(orOnce2){
            orOnce2 = false;
            var distence = dom.offsetTop + 40;
            hideClass.scrollTop = 0;
            hideClass.style.height = "314px";
            hideClass.style.top = distence + "px";
            ajax({
                url:"global_region.json",
                method:"get",
                dataType:"json",
                success:function(data){
                    var obj = JSON.parse(data);
                    var arr = obj.RECORDS;
                    var html = "";
                    for(var i = 0; i < arr.length; i++){
                        if(arr[i].parent_id == cityParent){
                        	console.log(arr[i])
                            html += '<div data-parentId="'+arr[i].parent_id+'" data-regionId="'+arr[i].region_id+'">'+arr[i].region_name+'</div>';
                        }
                    }
                    hideClass.innerHTML = html;
                    var citys = hideClass.querySelectorAll("div");
                    for(var j = 0; j < citys.length; j++){
                        Tap(citys[j],function(e,dom){
                            var m1 = document.querySelector(".m2")
                            m1.style.transform = "rotate(0)";
                            var provName = dom.innerHTML;
                            city.innerHTML = provName;
                            countyParent = dom.getAttribute("data-regionId");
                            hideClass.style.height = "0";
                            county.innerHTML = "请选择区县";
                            finalid = null;
                        });
                    }
                }
            });
        }else{
            orOnce2 = true;
            hideClass.style.height = "0";
            dom.nextSibling.nextSibling.style.transform = "rotate(0)";
        }

    });
    Tap(county,function(e,dom){
        dom.nextSibling.nextSibling.style.transform = "rotate(-180deg)";
        if(orOnce3){
            orOnce3 = false;
            var distence = dom.offsetTop + 40;
            hideClass.scrollTop = 0;
            hideClass.style.height = "314px";
            hideClass.style.top = distence + "px";
            ajax({
                url:"global_region.json",
                method:"get",
                dataType:"json",
                success:function(data){
                    var obj = JSON.parse(data);
                    var arr = obj.RECORDS;
                    var html = "";
                    for(var i = 0; i < arr.length; i++){
                        if(arr[i].parent_id == countyParent){
                            html += '<div data-parentId="'+arr[i].parent_id+'" data-regionId="'+arr[i].region_id+'">'+arr[i].region_name+'</div>';
                        }
                    }
                    hideClass.innerHTML = html;
                    var countys = hideClass.querySelectorAll("div");
                    for(var j = 0; j < countys.length; j++){
                        Tap(countys[j],function(e,dom){
                            var m1 = document.querySelector(".m3")
                            m1.style.transform = "rotate(0)";
                            var provName = dom.innerHTML;
                            county.innerHTML = provName;
                            finalid = dom.getAttribute("data-regionId");
                            hideClass.style.height = "0";
                        });
                    }
                }
            });
        }else{
            orOnce3 = true;
            dom.nextSibling.nextSibling.style.transform = "rotate(0)";
            hideClass.style.height = "0";
        }

    });
}
window.onload = function(){
    chooseRegion();
    var save = document.querySelector("footer");
    var token = selectByKey("like_token");
    console.log(token)
    Tap(save,function(){
        var dpost = document.getElementById("dpost");
        var name = dpost.children[0].value;
        var phone = dpost.children[1].value;
        var address = dpost.children[dpost.children.length-1].value;
        var addrId = countyParent || cityParent;
        console.log(name,phone,address,addrId)
        if(name && phone && address && addrId){
            $.ajax({
                url:pathName+"/api/v1/member/cart/122/address",
                type:"post",
                dataType:"json",
                headers:{
                    "Accept":"application/json",
                    "Authorization":"Bearer "+token,
                },
                data:{
                    "data":{
                        "name":name,
                        "address":address,
                        "area_id":addrId,
                        "phone":phone
                    }
                },
                success:function(data){
                    console.log(data);
                    window.location.href = "../allorder/torders.html";
                }
            })
        }else{
            alert("请完善您的信息");
        }

    });
}